using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace pixtek
{
	/// <summary>
	/// Summary description for DoubleBufferPaint.
	/// </summary>
	public class DoubleBufferPaint : System.Windows.Forms.UserControl
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private Bitmap		image;
		private Graphics	dc;

		public DoubleBufferPaint()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
				if (image!=null)
					image.Dispose();
				if (dc!=null)
					dc.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// DoubleBufferPaint
			// 
			this.Name = "DoubleBufferPaint";
			this.SizeChanged += new System.EventHandler(this.DoubleBufferPaint_SizeChanged);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.DoubleBufferPaint_Paint);

		}
		#endregion

		private void DoubleBufferPaint_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			if (dc!=null)
			{
				DoPainting (dc, ClientRectangle, 0);
				e.Graphics.DrawImageUnscaled(image, 0, 0);
			}
			else
				e.Graphics.Clear(Color.White);
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{
		}

		/// <summary>
		/// Direct painting
		/// </summary>
		protected void DirectPaint(int level)
		{
			if (dc!=null)
			{
				DoPainting (dc, ClientRectangle, level);
				Graphics tmp = CreateGraphics();
				tmp.DrawImageUnscaled(image, 0, 0);
				tmp.Dispose();
			}
		}

		protected void DirectPaint()
		{
			DirectPaint(0);
		}

		/// <summary>
		/// Override this method when need to paint
		/// </summary>
		/// <param name="dc">Drawing context</param>
		/// <param name="clientRectangle">Paint area</param>
		protected virtual void DoPainting (Graphics dc, Rectangle clientRectangle, int level)
		{
			Pen pen = new Pen(Color.Blue);
			dc.Clear(BackColor);
			dc.DrawLine(pen, clientRectangle.Left, clientRectangle.Top, clientRectangle.Right, clientRectangle.Bottom);
			dc.DrawLine(pen, clientRectangle.Right, clientRectangle.Top, clientRectangle.Left, clientRectangle.Bottom);
		}

		protected virtual void OnSizeChanged ()
		{
		}

		private void DoubleBufferPaint_SizeChanged(object sender, System.EventArgs e)
		{
			OnSizeChanged();
			if (ClientRectangle.Width>0 && ClientRectangle.Height>0)
			{
				image = new Bitmap (ClientRectangle.Width, ClientRectangle.Height);
				dc = Graphics.FromImage(image);
				DirectPaint(1);
			}
		}
	}
}
